require 'win32ole'
require 'test\unit\assertions'
include Test::Unit::Assertions

class AutoIt
  def initialize(init_parameter=nil)
    @autoit = WIN32OLE.new "AutoItX3.Control"
  end

  def type(target, value)
    @autoit.ControlSetText(target[:title.to_s], target[:text.to_s], target[:controlID.to_s], value)
  end

  def click(target, null)
    @autoit.ControlClick(target[:title.to_s], target[:text.to_s], target[:controlID.to_s], target[:button.to_s], target[:clicks.to_s], target[:x.to_s], target[:y.to_s])
  end

  def send_key(null, value)
    @autoit.Send(value)
  end

  def run(null, value)
    @autoit.Run(value)
    sleep(2000)
  end

  def sleep(time)
    @autoit.sleep(time)
  end

  def win_wait_active(win_title, win_text, timeout)
    raise "ERROR: Window #{win_title} is not found." unless @autoit.WinWaitActive(win_title, win_text, timeout)
    sleep(1000)
  end

  def win_activate(win_title, win_text)
    @autoit.WinActivate(win_title, win_text)
  end

  def close(win_title)
    @autoit.WinClose(win_title)
  end

  def kill_process(process_name)
    @autoit.ProcessClose(process_name)
  end

  def get_window_text(win_title)
    @autoit.WinGetText(win_title)
  end

  def win_maximize(win_title)
    @autoit.WinSetState(win_title, "", @autoit.SW_MAXIMIZE)
  end

  def win_menu_click(title, text, menu_item, sub_menu_item)
    @autoit.WinMenuSelectItem(title, text, menu_item, sub_menu_item)
  end

  def assert_text(target, value)
    assert_equal value, @autoit.ControlGetText(target[:title], target[:text], target[:controlID])
  end

end

